<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定时器</title>
</head>
<body>
    <div id="outer">1</div>

    <button type="button" onclick="stop()">停止</button>

    <script>
        let outer = document.getElementById("outer");

        function add(){
            outer.innerHTML=parseInt(outer.innerHTML.trim())+1;
        }

        let st = setInterval(add,1000);

        function stop(){
            //清除定时器
            clearInterval(st);
        }
    </script>
</body>
</html>